package com.zhuang.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhuang.dto.NotificationDTO;
import com.zhuang.dto.QuestionDTO;
import com.zhuang.mapper.CommentMapper;
import com.zhuang.mapper.NotificationExtMapper;
import com.zhuang.mapper.NotificationMapper;
import com.zhuang.mapper.QuestionExtMapper;
import com.zhuang.pojo.Comment;
import com.zhuang.pojo.Notification;
import com.zhuang.pojo.Question;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class NotificationService {
    @Autowired
    NotificationExtMapper notificationExtMapper;

    @Autowired
    QuestionExtMapper questionExtMapper;

    @Autowired
    CommentMapper commentMapper;
    @Autowired
    NotificationMapper notificationMapper;

    public  PageInfo<NotificationDTO> list(Long receiver,Integer currentPage,Integer pageSize) {
        PageHelper.startPage(currentPage, pageSize);
        List<NotificationDTO> list = notificationExtMapper.list(receiver);
        PageInfo<NotificationDTO> notificationDTOList = new PageInfo<>(list);
        for (NotificationDTO notificationDTO : notificationDTOList.getList()) {
            Integer type = notificationDTO.getType();
            if (type == 1) {
                Question question = questionExtMapper.selectByOuterId(notificationDTO.getOuterid().intValue());
                notificationDTO.setQuestion(question);
            } else {
                Comment comment = commentMapper.selectByPrimaryKey(notificationDTO.getOuterid());
                Question question = questionExtMapper.selectByOuterId(comment.getParentId().intValue());
                notificationDTO.setQuestion(question);
                notificationDTO.setComment(comment);
            }
        }
        return notificationDTOList;

    }


    public void updateStatus(Integer questionId) {
        notificationExtMapper.update(questionId.longValue());
    }

    public Integer count(long receiver) {
        Integer count = notificationExtMapper.count(receiver);
        return count;
    }
}
